﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.ReceptionGoods
{
    public partial class ShiftForm : Form
    {
        private int _shiftDepartmentId;
        private int _docId;
        private int shiftDepartmentId
        {
            get
            {
                SyncDeptInfo sdi = cbDepartments.SelectedItem as SyncDeptInfo;
                if (sdi == null)
                    throw new Exception();
                return sdi.Id;
            }
        }
        private string SyncHostName
        {
            get
            {
                SyncDeptInfo sdi = cbDepartments.SelectedItem as SyncDeptInfo;
                if (sdi == null)
                    throw new Exception();
                return sdi.SyncHostName;
            }
        }
        public ShiftForm(int docId)
        {
            _docId = docId;
            InitializeComponent();
            cbDepartments.DisplayMember = "Name";
            cbDepartments.ValueMember = "Id";
            cbDepartments.DataSource = SyncDeptInfo.GetList();
        }

        private void ShiftForm_Load(object sender, EventArgs e)
        {

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            NewSync.Data.Common.ConnectionString = Properties.Settings.Default.DIAptekaConnectionString;
            NewSync.Runner r = new NewSync.Runner();
            this.Cursor = Cursors.WaitCursor;
            r.FromArrivalToInternalCharge(_docId, shiftDepartmentId, FirstForm.Login, SyncHostName);
            this.Cursor = Cursors.Default;
            this.DialogResult = DialogResult.OK;
        }
    }
    public class SyncDeptInfo
    {
        private Int32 _i;

        public Int32 Id
        {
            get { return _i; }
            set { _i = value; }
        }
        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        private string _syncHostName;

        public string SyncHostName
        {
            get { return _syncHostName; }
            set { _syncHostName = value; }
        }
        public static List<SyncDeptInfo> GetList()
        {
            string QueryString = @"SELECT d.Id, d.Name,d.SyncHostName
FROM CO.Departments AS d
ORDER BY 2";
            try
            {
                List<SyncDeptInfo> res = new List<SyncDeptInfo>();
                using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(Properties.Settings.Default.DIAptekaConnectionString))
                {
                    System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(QueryString, connection);
                    connection.Open();
                    System.Data.SqlClient.SqlDataReader r = command.ExecuteReader();
                    Int32 iid = r.GetOrdinal("Id");
                    Int32 iName = r.GetOrdinal("Name");
                    Int32 iSyncHostName = r.GetOrdinal("SyncHostName");
                    while (r.Read())
                    {
                        SyncDeptInfo sdi = new SyncDeptInfo();
                        sdi.Id = r.GetInt32(iid);
                        sdi.Name = r.GetString(iName);
                        if (!r.IsDBNull(iSyncHostName))
                            sdi.SyncHostName = r.GetString(iSyncHostName);
                        res.Add(sdi);
                    }
                }
                return res;
            }
            catch (System.Data.SqlClient.SqlException se)
            {
                string s = se.ToString();
                throw se;
            }
            catch (Exception ee)
            {
                string s = ee.ToString();
                throw ee;
            }

        }

    }
}